package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.util.DataFileUtil;
import com.fdb.efp.nls.service.dao.BatComdetailDao;
import com.fdb.efp.nls.service.domain.BatComdetail;
import com.fdb.efp.nls.service.facade.BatComdetailService;
import com.fdb.efp.nls.service.vo.BatComdetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>ServiceImpl: 日终交易明细表				<p>
 * @author chenqm1
 * @date 2018-05-31
 */
@Service("batComdetailService")
public class BatComdetailServiceImpl implements BatComdetailService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BatComdetailServiceImpl.class);

	@Autowired
	private BatComdetailDao batComdetailDao;
	
	/**
	 * 新增操作
	 */
	public int insertBatComdetail(BatComdetailVO inBatComdetailVo) {
		logger.debug("当前新增数据为:"+ inBatComdetailVo.toString());
		int num = 0;
		try {
			BatComdetail batComdetail = new BatComdetail();
			beanCopy(inBatComdetailVo, batComdetail);
			num = batComdetailDao.insertBatComdetail(batComdetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}
	
	
	@Override
	public boolean fileDataInsertTable(File file, BatComdetailVO clazz, String operation) throws Exception{
		boolean result = false;
		try {
			List<BatComdetailVO> readTxt = DataFileUtil.readTxt(file,clazz.getClass(),"batComdetail.json","GB2312");
			if(readTxt != null && readTxt.size() > 0) {
				int sum = readTxt.size();
				logger.info("文件中总数据量为:"+sum);
				int batchNum = BaseConstant.batchInsertNum_5000;
				logger.info("分批处理设置每次处理条数:"+batchNum);
				int cycleNum = sum % batchNum == 0 ? sum/batchNum : sum/batchNum +1;
				logger.info("需要执行的处理次数:" + cycleNum);
				int insertNum = 0;
				for(int i=0;i<cycleNum;i++) {
					logger.info("开始执行第"+(i+1)+"次处理");
					int start = i*batchNum; 
					int end = start + batchNum;
					logger.info("查询数据范围:"+(start+1)+"~"+end);
					List<BatComdetailVO> subList = new ArrayList<BatComdetailVO>();
					if(end > readTxt.size()) {
						subList = readTxt.subList(start, readTxt.size());
					}else {
						subList = readTxt.subList(start, end);
					}
					int num = batComdetailDao.batchInsertBatComdetail(subList);
					insertNum = insertNum + num;
				}
				logger.info("文件中数据成功插入"+insertNum);
				result = true;
			}
		} catch (Exception e) {
			logger.error("插入中间表bat_comdetail出错!", e);
			throw new Exception("插入中间表bat_comdetail出错！"+e);
		}
		return result;
	}

	@Override
	public List<BatComdetailVO> queryDataByAccount() {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BatComdetailVO> list = null;
		try {
			List<BatComdetail> batComdetails = batComdetailDao.queryDataByAccount();
			logger.debug("当前查询结果集数量为:"+ batComdetails.size());
			list = (List<BatComdetailVO>) beansCopy(batComdetails, BatComdetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}


	@Override
	public void deleteAll() {
		try {
			batComdetailDao.deleteAll();
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
		}
	}

}
